package com.zhang.activity;

import com.zhang.little_sample.R;

import android.R.integer;
import android.app.Dialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.widget.ImageView;
import android.widget.LinearLayout.LayoutParams;
import android.widget.RelativeLayout;

public class CircleAdjustButton extends Dialog {
	public interface OnCircleAdjustListener {
		public void back(int resultTag);
	}

	private OnCircleAdjustListener onCircleAdjustListener;
	private Context context;
	private RelativeLayout mLayout;
	// 触摸点
	float startX;// 触摸点的x
	float startY;// 触摸点的y
	// 圆心
	float circularX;
	float circularY;
	int width;
	int height;
	private Bitmap bitmap;
	private ImageView imageView;

	public CircleAdjustButton(Context context,
			OnCircleAdjustListener onCircleAdjustListener) {
		super(context);
		this.context = context;
		this.onCircleAdjustListener = onCircleAdjustListener;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.circle_adjust_button);
		initChildViews();
	}

	// 初始化控件
	private void initChildViews() {
		mLayout = (RelativeLayout) findViewById(R.id.relative_circle_adjust);
		imageView = (ImageView) findViewById(R.id.img_circle_adjust_btn);
		
		bitmap = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.icon_circle_button);
		imageView.setImageBitmap(bitmap);
		imageView.setLayoutParams(new LayoutParams(bitmap.getWidth(), bitmap.getHeight()));
		// 圆心
		imageView.setOnTouchListener(new RotateAdjustOnTouchListener());
	}

	private class RotateAdjustOnTouchListener implements OnTouchListener {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			int eventAction = event.getAction();
			width = v.getWidth();
			height = v.getHeight();
			if (width != 0 && height != 0) {
				circularX = width / 2;
				circularY = height / 2;
			} else {
				circularX = 0;
				circularY = 0;
			}
			switch (eventAction) {
			case MotionEvent.ACTION_DOWN:
				// 获取触摸点的坐标
				startX = event.getX();
				startY = event.getY();
				break;
			case MotionEvent.ACTION_MOVE:
				// 获取实时触摸点坐标
				float nowX;
				float nowY;
				nowX = event.getX();
				nowY = event.getY();
				// 计算与原始点造成的角度
				// 角度的限制条件
				int angle = getAngle(startX, startY, nowX, nowY, circularX,
						circularY);
				if (angle >= 180) {
					angle = 180;
				}
				// 根据角度旋转bitmap
				bitmap = rotate(bitmap, angle, width, height);
				// 重绘bitmap并显示
				imageView.setImageBitmap(bitmap);
				v.postInvalidate();
				break;
			case MotionEvent.ACTION_UP:
				// 给出音量百分比
				CircleAdjustButton.this.dismiss();
				break;

			default:
				break;
			}
			return true;
		}
	}

	/**
	 * 计算旋转角度
	 * 
	 * @param startX
	 * @param startY初始点
	 * @param nowX现在点
	 * @param nowY
	 * @param circularX圆心
	 * @param circularY
	 * @return 返回float浮点型 (弧度)
	 */
	private int getAngle(float startX, float startY, float nowX, float nowY,
			float circularX, float circularY) {
		int angle = 0;
		double a = Math.sqrt((startX - circularX) * (startX - circularX)
				+ (startY - circularY) * (startY - circularY));
		double b = Math.sqrt((nowX - circularX) * (nowX - circularX)
				+ (nowY - circularY) * (nowY - circularY));
		double c = Math.sqrt((startX - nowX) * (startX - nowX)
				+ (startY - nowY) * (startY - nowY));
		double angledouble = Math.acos((a * a + b * b - c * c) / (2.0 * a * b));
		angle = (int) Math.toDegrees(angledouble);
		return angle;
	}

	/**
	 * 根据角度旋转图片
	 * 
	 * @param b
	 * @param degrees
	 * @return
	 */
	private Bitmap rotate(Bitmap b, int degrees, int width, int height) {
		if (degrees != 0 && b != null) {
			Matrix m = new Matrix();
			m.setRotate(degrees, circularX, circularY);
			try {
				Bitmap b2 = Bitmap
						.createBitmap(b, 0, 0, width, height, m, true);
				if (b != b2) {
					b.recycle(); // Android开发网再次提示Bitmap操作完应该显示的释放
					b = b2;
				}
			} catch (OutOfMemoryError ex) {
				// 建议大家如何出现了内存不足异常，最好return 原始的bitmap对象。.
			}
		}
		return b;
	}
}
